package io.renren.param;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.renren.common.utils.Constant;
import io.renren.entity.*;
import io.renren.utils.DateTool;
import lombok.Data;
import org.springframework.beans.BeanUtils;

@Data
public class RefreshEslParam {
    /**
     * 价签类型，doorPlate-门牌；namePlate-铭牌
     */
    String eslType;

    @JsonProperty(value = "esl_id")
    String esl_id;

    @JsonProperty(value = "sid")
    String sid;
    /**
     * 操作人的企业微信id
     */
    String userId;

    /**
     * 公司id
     */
    String compId;

    /**
     * 会议申请记录id
     */
    String applicationId;

    /**
     * 会议室id
     */
    String roomId;

    @JsonProperty(value = "priority")
    Integer priority;

    @JsonProperty(value = "back_url")
    String back_url;

    @JsonProperty(value = "template")
    String template;


    //桌面铭牌参数
    @JsonProperty(value = "CompanyName")
    String CompanyName;

    @JsonProperty(value = "post")
    String post;
    /**
     * 姓名
     */
    @JsonProperty(value = "name")
    String name;

    // 会议室门牌参数
    /**
     * 会议室名称
     */
    @JsonProperty(value = "theme")
    String theme;

    /**
     * 会议室使用状态
     */
    @JsonProperty(value = "status")
    String status;

    /**
     * 当前会议
     */
    @JsonProperty(value = "NowMeeting")
    String NowMeeting;
    /**
     * 当前会议预约人
     */
    @JsonProperty(value = "BookingPerson1")
    String BookingPerson1;
    /**
     * 下一会议
     */
    @JsonProperty(value = "Nextmeeting")
    String Nextmeeting;
    /**
     * 下一会议预约人
     */
    @JsonProperty(value = "BookingPerson2")
    String BookingPerson2;
    /**
     * 当前会议开始时间
     */
    @JsonProperty(value = "date1")
    String date1;
    /**
     * 当前会议结束时间
     */
    @JsonProperty(value = "date2")
    String date2;
    /**
     * 下一会议开始时间
     */
    @JsonProperty(value = "date3")
    String date3;
    /**
     * 下一会议结束时间
     */
    @JsonProperty(value = "date4")
    String date4;
    /**
     * 二维码
     */
    @JsonProperty(value = "qrCode")
    String qrCode;
    /**
     * 公司logo
     */
    @JsonProperty(value = "compLogo")
    String compLogo;
    /**
     * 当天日期星期
     */
    @JsonProperty(value = "DateTime")
    private String DateTime;

    //工位参数
    /**
     * 工位预约人英文姓名
     */
    private String EnglishName;
    /**
     * 工号
     */
    private String JobNumber;
    /**
     * 部门
     */
    private String department;
    /**
     * 部门英文名
     */
    private String enDepartment;
    /**
     * 工位预定状态
     */
    private String order;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 工位编号
     */
    private String SeatNumber;


    private JSONObject store_config;

    /**
     * 是否党员
     */
    private Boolean isPartyMember;

    public static RefreshEslParam convertForSeatPlate(SeatApplicationEntity seatApplicationEntity, SeatEslEntity seatEslEntity, SeatEntity seatEntity, String template, String seatQrCode) {
        RefreshEslParam param = new RefreshEslParam();
        BeanUtils.copyProperties(seatApplicationEntity, param);

        param.setEsl_id(seatEslEntity.getEslId());
        param.setSid(String.valueOf(RandomUtil.randomNumbers(10)));
        param.setPriority(10);
        param.setBack_url(Constant.EW_CALL_BACK_URL);
        param.setTemplate(template);
        param.setCompId(seatEntity.getCompId());

        if (Constant.ApplicationState.OVER.getValue().equals(seatApplicationEntity.getState())) {
            param.setName("");
            param.setJobNumber("");
            param.setDepartment("");
            param.setQrCode(seatQrCode);
        } else {
            param.setName(seatApplicationEntity.getEmployeeName());
            param.setJobNumber(seatApplicationEntity.getEmployeeCode());
            param.setDepartment(seatApplicationEntity.getDepartment());
            param.setQrCode(seatQrCode);
            String startDateTimeStr = DateUtil.format(seatApplicationEntity.getStartTime(), DatePattern.NORM_DATETIME_MINUTE_FORMAT);
            String endDateTimeStr = seatApplicationEntity.getStartTime().getHours() + ":" + seatApplicationEntity.getStartTime().getMinutes();
            param.setDateTime(startDateTimeStr + " - " + endDateTimeStr);
            param.setOrder("已预定");
            param.setStatus("签到");
            param.setSeatNumber(seatEntity.getCode());
            String startTime = DateUtil.format(seatApplicationEntity.getStartTime(), DatePattern.NORM_DATETIME_FORMAT);
            String endTime = seatApplicationEntity.getEndTime().getHours() + ":" + seatApplicationEntity.getEndTime().getMinutes();
            String dateTime = startTime + "-" + endTime;
            param.setDateTime(dateTime);
            param.setIsPartyMember(seatEntity.getIsPartyMember());
        }

        return param;
    }

    public static RefreshEslParam convertForNamePlate(EslContentEntity eslContentEntity, String compId, String template) {
        RefreshEslParam param = new RefreshEslParam();
        BeanUtils.copyProperties(eslContentEntity, param);

        param.setCompId(compId);
        param.setEsl_id(eslContentEntity.getEslId());
        param.setName(eslContentEntity.getName());
        param.setPriority(10);
        param.setSid(String.valueOf(RandomUtil.randomNumbers(10)));
        param.setTemplate(template);

        return param;
    }

    public static RefreshEslParam convertForDoorPlate(RoomEslEntity roomEslEntity, String template
            , String roomName, ApplicationEntity nowApplication, ApplicationEntity nextMeeting, String roomQrCode) {
        RefreshEslParam param = new RefreshEslParam();
        BeanUtils.copyProperties(roomEslEntity, param);
        param.setCompId(nowApplication.getCompId());
        param.setEslType("doorPlate");

        param.setEsl_id(roomEslEntity.getEslId());
        param.setSid(String.valueOf(RandomUtil.randomNumbers(10)));
        param.setPriority(10);
        param.setBack_url(Constant.EW_CALL_BACK_URL);
        param.setTemplate(template);
        param.setTheme(roomName);

        if (null != nextMeeting) {
            param.setNextmeeting(nextMeeting.getSubject());
            param.setBookingPerson2(nextMeeting.getEmployeeName());
        }

        Integer startMin = nowApplication.getStartTime().getMinutes();
        String startMinStr;
        if (startMin == 0) {
            startMinStr = startMin + "0";
        } else {
            startMinStr = startMin + "";
        }
        Integer endMin = nowApplication.getEndTime().getMinutes();
        String endMinStr;
        if (endMin == 0) {
            endMinStr = endMin + "0";
        } else {
            endMinStr = endMin + "";
        }

        if (Constant.ApplicationState.OVER.getValue().equals(nowApplication.getState())) {
            param.setStatus(Constant.RoomState.IDLE.getValue());
            param.setNowMeeting("");
            param.setBookingPerson1("");
            param.setDate1("");
            param.setDate2("");
        } else {
            param.setStatus(Constant.RoomState.USING.getValue());
            param.setNowMeeting(nowApplication.getSubject());
            param.setBookingPerson1(nowApplication.getEmployeeName());
            param.setDate1(nowApplication.getStartTime().getHours() + startMinStr);
            param.setDate2(nowApplication.getEndTime().getHours() + endMinStr);
        }

        if (null != nextMeeting && nextMeeting.getId() != null) {
            Integer nextStartMin = nextMeeting.getStartTime().getMinutes();
            String nextStartMinStr;
            if (nextStartMin == 0) {
                nextStartMinStr = nextStartMin + "0";
            } else {
                nextStartMinStr = nextStartMin + "";
            }
            Integer nextEndMin = nextMeeting.getEndTime().getMinutes();
            String nextEndMinStr;
            if (nextEndMin == 0) {
                nextEndMinStr = nextEndMin + "0";
            } else {
                nextEndMinStr = nextEndMin + "";
            }
            param.setDate3(nextMeeting.getStartTime().getHours() + nextStartMinStr);
            param.setDate4(nextMeeting.getEndTime().getHours() + nextEndMinStr);
        }
        param.setQrCode(roomQrCode);
        param.setDateTime(DateUtil.today() + " 星期" + DateTool.dayNumberToCharater(DateUtil.thisDayOfWeek()));

        return param;
    }
}
